package com.hc.controller;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.constant.UserConst;
import com.hc.domain.ReceiveAddr;
import com.hc.domain.User;
import com.hc.service.ReceiveAddrService;
import com.hc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author hecai
 * @date 2021/12/23 14:37
 */
@Slf4j
@RestController
@RequestMapping("/receiveAddr")
public class ReceiveAddrController {

    @Resource
    private ReceiveAddrService receiveAddrService;

    @PostMapping
    public Result create(@RequestBody ReceiveAddr receiveAddr) {
        boolean res = receiveAddrService.save(receiveAddr);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result updateById(@RequestBody ReceiveAddr receiveAddr) {
        boolean res = receiveAddrService.updateById(receiveAddr);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result removeById(@PathVariable Serializable id) {
        boolean res = receiveAddrService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Serializable id) {
        ReceiveAddr res = receiveAddrService.getById(id);
        return ResultUtil.success("查询成功").setData(res);
    }

    //@PostMapping
    //public Result getByCondition(@RequestBody ReceiveAddrQuery condition) {
    //    List<ReceiveAddr> list = receiveAddrService.queryByCondition(condition);
    //    return ResultUtil.success("查询成功").setData(list);
    //}

    @Resource
    private UserService userService;

    /**
     * 添加收货地址
     *
     * @param receiveAddr
     * @return
     */
    @PostMapping("/receiveAddr/add")
    public Result addReceiveAddr(@RequestBody ReceiveAddr receiveAddr) {
        User user = UserConst.user;
        //获取用户已有的地址数量
        Integer receiveAddrs = userService.getById(user.getId()).getReceiveAddrs();
        if (receiveAddrs >= UserConst.RECEIVE_ADDR_AMOUNT) {
            return ResultUtil.success("地址数量超限，请删除不常用的地址，再添加");
        }

        boolean saveRes = receiveAddrService.saveAddr(receiveAddr);
        if (saveRes) {
            return ResultUtil.success("地址添加成功");
        } else {
            return ResultUtil.fail("地址添加失败");
        }
    }

    /**
     * 查找指定用户的收件地址列表
     *
     * @return
     */
    @GetMapping("/receiveAddr/list")
    public Result listReceiveAddrs() {
        List<ReceiveAddr> receiveAddrList = receiveAddrService.getReceiveAddrs();
        return ResultUtil.success().setData(receiveAddrList);
    }

    /**
     * 修改收货地址
     */
    @PostMapping("/receiveAddr/update")
    public Result updateReceiveAddr(@RequestBody ReceiveAddr receiveAddr) {
        boolean updateRes = receiveAddrService.updateById(receiveAddr);
        if (updateRes) {
            return ResultUtil.success("地址修改成功");
        } else {
            return ResultUtil.fail("地址修改失败");
        }
    }
}